package com.example.community.dao;

import com.example.community.mapper.RepairMapper;
import com.example.community.mapper.RoomMapper;
import com.example.community.mapper.UnitMapper;
import com.example.community.model.Repair;
import com.example.community.model.Room;
import com.example.community.model.Unit;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 *维修管理表
 */
public class RepairDao {
    public Repair fandById(Integer id){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            return mapper.selectByPrimaryKey(id);
        }
    }

    public List<Repair> fandByAll(){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            return mapper.selectAll();
        }
    }

    public List<Repair> fandByAllStatic(){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            return mapper.selectAllStatic();
        }
    }

    public List<Repair> fandByAllByUsNo(Integer usNo){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            return mapper.selectAllByUsNo(usNo);
        }
    }

    public List<Repair> fandByAllByUsNoStatic(Integer usNo){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            return mapper.selectAllByUsNoStatic(usNo);
        }
    }

    public List<Repair> fandLimit(Integer ys, Integer shuju){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            Integer yss =(ys - 1) * shuju;
            return mapper.selectLimit(yss,shuju);
        }
    }

    public Integer selectLimitCount(Integer pageSize){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            int count = mapper.CountRepair();
            return (count + pageSize - 1)/pageSize;
        }
    }

    public Integer insert(Repair repair){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            int i = mapper.insert(repair);
            session.commit();
            return i;
        }
    }

    public Integer update(Repair repair){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            int i = mapper.updateByPrimaryKey(repair);
            session.commit();
            return i;
        }
    }

    public Integer delete(Integer id){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            int i = mapper.deleteByPrimaryKey(id);
            session.commit();
            return i;
        }
    }

    public List<Repair> fandLimitByUsNo(Integer ys, Integer shuju,Integer usNo){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            Integer yss =(ys - 1) * shuju;
            return mapper.selectLimitByUsNo(yss,shuju,usNo);
        }
    }

    public Integer selectLimitCountByUsNo(Integer pageSize,Integer usNo){
        try (SqlSession session = MybatisUtil.getSession()){
            final RepairMapper mapper = session.getMapper(RepairMapper.class);
            int count = mapper.CountRepairByUsNo(usNo);
            return (count + pageSize - 1)/pageSize;
        }
    }
}
